package se.grp03.fmtuner.scheduler;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Manages the list of channels retrieved from the
 * FM Tuner Agent, and is used by the Scheduler.
 * @author David DeGroate
 */
public class ChannelManager {
	private static ChannelManager cm; // Singleton
	private static ArrayList<Double> listOfChannels;
	
	private ChannelManager() {
		clearChannels();
		addChannel(123.45); // <-- DEBUG ---------------------------------------
		addChannel(678.90); // <-- DEBUG ---------------------------------------
	}
	
	/**
	 * Gives a reference to the single instance of
	 * the ChannelManager singleton.
	 * @return A reference to the ChannelManager.
	 */
	public static ChannelManager getInstance() {            
		/*
		 * If there is no instance of ChannelManager, 
		 * then create one and only one instance.
		 */
		if(cm == null)
			cm = new ChannelManager();
		return cm;
	}
	
	/**
	 * Stores a channel in the local list of channels.
	 * @param channel - The FM radio frequency.
	 */
	public void addChannel(Double channel) {
		listOfChannels.add(channel);
	}
	
	/**
	 * Clears the local list of channels, in order to
	 * remove old channels to maintain a fresh list.
	 */
	public void clearChannels() {
		listOfChannels = new ArrayList<Double>();
	}
	
	/**
	 * Retrieves the list of all stored channels.
	 * @return An ArrayList<String> of all stored channels.
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Double> getChannels() {
		return (ArrayList<Double>) listOfChannels.clone();
	}
	
	/**
	 * Retrieves the list of all stored channels, as a string.
	 * @return A delimited String of all channels.
	 */
	public String getChannelsAsString() {
		DecimalFormat df = new DecimalFormat("##0.00");
		StringBuilder sb = new StringBuilder();
		for(Double c : listOfChannels) {
			sb.append(df.format(c));
			sb.append(" ");
		}
		return sb.toString();
	}
}
